/**
 * @Author yinyunqi
 * @date 2018年1月30日
 * @Content 
 */
package com.yongqi.controller.console;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.Family;
import com.yongqi.model.back.Email;
import com.yongqi.model.back.Message;
import com.yongqi.pub.PrincipalFormat;
import com.yongqi.service.back.EmailService;
import com.yongqi.service.back.MessageService;

@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    MessageService messageService;
    
    @RequestMapping("/msgTest")
    public void msgTest() {
	Map<String, String> map = new HashMap<String, String>();
	map.put("familyAccount", PrincipalFormat.currentFA());
    }
    /**
     * 定时刷新推送信息
     */
    @ResponseBody
    @RequestMapping("/reloadPushMsg")
    public String reloadPushMsg() {
	Map<String, String> map = new HashMap<String, String>();
	map.put("familyAccount", PrincipalFormat.currentFA());
	List<Message> list = messageService.msgPushQuery(map);
	System.out.println("reloadPushMsg--------------------->"+list.size());
	String msg_content = "";
	for(Message message : list) {
	    msg_content += "<a href=\"#\"><i class=\"fa fa-users text-aqua\"></i><span>"+message.getContent()+"</span></a>";
	}
	JSONObject object = new JSONObject();
	object.put("push_msg_amount", list.size());
	object.put("push_msg", msg_content);
	return object.toJSONString();
    }
    
	@RequestMapping("/index")
	public String emailIndex() {
		return "msg/msgIndex";
	}
	@ResponseBody
	@RequestMapping("/query")
	public String msgQuery(HttpServletRequest request,@RequestParam int pageNumber ,int pageSize) {
		Map<String, String> map = new HashMap<String, String>();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		map.put("familyAccount", familyAccount);
		int total = messageService.msgQuery(map).size();
		PageHelper.startPage(pageNumber,pageSize);
		List<Message> emails = messageService.msgQuery(map);
		JSONObject object = new JSONObject();
		object.put("rows", emails);
		object.put("total", total);
		return object.toJSONString();
	}
	/**
	 * 修改邮件状态
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/changeReaded")
	public String changeReaded(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		String id = request.getParameter("id");
		map.put("familyAccount", familyAccount);
		map.put("id", id);
		messageService.changeReaded(map);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
	/**
	 * 删除邮件
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/deleteMsg")
	public String deleteEmail(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		String id = request.getParameter("id");
		map.put("familyAccount", familyAccount);
		map.put("id", id);
		messageService.backMsgDetele(map);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
}

